//
//  BookListDetailViewController.m
//  FunRead
//
//  Created by  King on 15/5/22.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "BookListDetailViewController.h"
#import "BookListModel.h"
#import "BookListDetail.h"
#import "BookListDetailTableViewCell.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "BookDetailController.h"
#import "MJRefresh.h"
@interface BookListDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) NSInteger i;
@end

@implementation BookListDetailViewController
- (void)dealloc
{
    [_tableArray release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    BookListModel *model = [[BookListModel alloc] init];
    model = [self.array objectAtIndex:_categoryTag];;
    self.navigationItem.title = model.categoryName;
    
    // 设置导航返回键图片
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backbefore.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
    self.navigationItem.leftBarButtonItem = button;
    [button release];
    
    //post请求网络数据
    NSString *str = [NSString stringWithFormat:@"%ld", self.categoryTag + 1];
    NSDictionary *dic = @{@"page":@"1", @"version":@"1.3", @"topId":str};
    __block BookListDetailViewController *book = self;
    [SANetWorkingTask request:@"http://vip.hentr.com/zhuibook/api/zhuibook/topList.do" parmaters:dic block:^(NSDictionary *result) {
        NSDictionary *dic = [result objectForKey:@"response"];
        book.tableArray = [dic objectForKey:@"ReturnData"];
        [book.tableView reloadData];
        
    }];
   [book createTableView];
    [book addHeader];
    [book addFooter];
}
- (void)action:(id)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 下拉刷新，上拉加载
- (void)addHeader
{
    __block BookListDetailViewController *bookList = self;
    // 添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
            [bookList.tableView reloadData];
            // 结束刷新
            [bookList.tableView headerEndRefreshing];
    }];
    
    //  自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
}
- (void)addFooter
{
    self.i = 2;
    // 添加上拉刷新尾部控件
    __block BookListDetailViewController *book = self;
    [self.tableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        NSString *str = [NSString stringWithFormat:@"%ld", self.categoryTag + 1];
        NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%ld", self.i], @"version":@"1.3", @"topId":str};
        
        [SANetWorkingTask request:@"http://vip.hentr.com/zhuibook/api/zhuibook/topList.do" parmaters:dic block:^(NSDictionary *result) {
            NSDictionary *dic = [result objectForKey:@"response"];
            NSMutableArray *array = [dic objectForKey:@"ReturnData"];
            for (NSDictionary *dic in array) {
                [book.tableArray addObject:dic];
            }
            book.i++;
            [book.tableView reloadData];
        }];
        [book.tableView.footer endRefreshing];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookDetailController *detail = [[BookDetailController alloc]init];
    detail.str = [[_tableArray objectAtIndex:indexPath.row] objectForKey:@"bookid"];
    
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 80;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorColor = [UIColor grayColor];
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    BookListDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[BookListDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
    }
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    NSDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    BookListDetail *detailModel = [[BookListDetail alloc] init];
    [detailModel setValuesForKeysWithDictionary:dic];
    
    
    NSURL *url = [NSURL URLWithString:detailModel.cover1];
    [cell.bookImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"无封面.gif"]];
    cell.bookName.text = detailModel.bookName;
    cell.shortIntro.text = detailModel.shortIntro;
    cell.author.text = [NSString stringWithFormat:@"👤 %@",detailModel.author];
    return cell;
}

@end
